import qt_shiyong_rc
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from mysql import mysql
import cv2
from get_face_encoding import get_encoding
from get_face_encoding import to_narray
from get_face_encoding import to_str
import face_recognition
from PyQt5.QtGui import *

class kaoqin(object):
    def setupUi(self, kaoqin):
        kaoqin.setObjectName("kaoqin")
        kaoqin.resize(791, 636)
        kaoqin.setMinimumSize(QtCore.QSize(791, 636))
        kaoqin.setMaximumSize(QtCore.QSize(791, 636))
        kaoqin.setStyleSheet("background-image: url(:/新前缀/好看的图片.jpg);")
        self.kaoqin_btn_kaoqin = QtWidgets.QPushButton(kaoqin)
        self.kaoqin_btn_kaoqin.setGeometry(QtCore.QRect(30, 500, 171, 111))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(16)
        self.kaoqin_btn_kaoqin.setFont(font)
        self.kaoqin_btn_kaoqin.setStyleSheet("image: url(:/新前缀/考勤.png);")
        self.kaoqin_btn_kaoqin.setText("")
        self.kaoqin_btn_kaoqin.setObjectName("kaoqin_btn_kaoqin")
        self.label = QtWidgets.QLabel(kaoqin)
        self.label.setGeometry(QtCore.QRect(90, 10, 471, 51))
        font = QtGui.QFont()
        font.setFamily("黑体")
        font.setPointSize(22)
        self.label.setFont(font)
        self.label.setObjectName("label")

        self.kaoqin_xiangji = QtWidgets.QLabel(kaoqin)
        
        self.kaoqin_btn_qingjia = QtWidgets.QPushButton(kaoqin)
        self.kaoqin_btn_qingjia.setGeometry(QtCore.QRect(560, 500, 171, 111))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(16)
        self.kaoqin_btn_qingjia.setFont(font)
        self.kaoqin_btn_qingjia.setStyleSheet("image: url(:/新前缀/请假 (1).png);")
        self.kaoqin_btn_qingjia.setText("")
        self.kaoqin_btn_qingjia.setObjectName("kaoqin_btn_qingjia")
        self.kaoqin_btn_buqian = QtWidgets.QPushButton(kaoqin)
        self.kaoqin_btn_buqian.setGeometry(QtCore.QRect(280, 500, 171, 111))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(16)
        self.kaoqin_btn_buqian.setFont(font)
        self.kaoqin_btn_buqian.setStyleSheet("image: url(:/新前缀/补签.png);")
        self.kaoqin_btn_buqian.setText("")
        self.kaoqin_btn_buqian.setObjectName("kaoqin_btn_buqian")
        self.kaoqin_shijian = QtWidgets.QLabel(kaoqin)
        self.kaoqin_shijian.setGeometry(QtCore.QRect(460, 30, 351, 61))
        font = QtGui.QFont()
        font.setFamily("方正姚体")
        font.setPointSize(16)
        self.kaoqin_shijian.setFont(font)
        self.kaoqin_shijian.setObjectName("kaoqin_shijian")
        self.label_3 = QtWidgets.QLabel(kaoqin)
        self.label_3.setGeometry(QtCore.QRect(550, 90, 231, 31))
        font = QtGui.QFont()
        font.setFamily("黑体")
        font.setPointSize(12)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.kaoqin_btn_xiangji = QtWidgets.QPushButton(kaoqin)
        self.kaoqin_btn_xiangji.setGeometry(QtCore.QRect(580, 340, 161, 121))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(16)
        self.kaoqin_btn_xiangji.setFont(font)
        self.kaoqin_btn_xiangji.setStyleSheet("image: url(:/新前缀/打开相机.png);")
        self.kaoqin_btn_xiangji.setText("")
        self.kaoqin_btn_xiangji.setObjectName("kaoqin_btn_xiangji")
        self.layoutWidget = QtWidgets.QWidget(kaoqin)
        self.layoutWidget.setGeometry(QtCore.QRect(560, 140, 191, 181))
        self.layoutWidget.setObjectName("layoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.number = QtWidgets.QLabel(self.layoutWidget)
        self.number.setMinimumSize(QtCore.QSize(50, 50))
        self.number.setObjectName("number")
        self.horizontalLayout_3.addWidget(self.number)
        self.kaoqin_xuehao = QtWidgets.QLineEdit(self.layoutWidget)
        self.kaoqin_xuehao.setObjectName("kaoqin_xuehao")
        self.horizontalLayout_3.addWidget(self.kaoqin_xuehao)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_5.setObjectName("horizontalLayout_5")
        self.number_3 = QtWidgets.QLabel(self.layoutWidget)
        self.number_3.setMinimumSize(QtCore.QSize(50, 50))
        self.number_3.setObjectName("number_3")
        self.horizontalLayout_5.addWidget(self.number_3)
        self.kaoqin_xingming = QtWidgets.QLineEdit(self.layoutWidget)
        self.kaoqin_xingming.setObjectName("kaoqin_xingming")
        self.horizontalLayout_5.addWidget(self.kaoqin_xingming)
        self.verticalLayout.addLayout(self.horizontalLayout_5)
        self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        self.number_2 = QtWidgets.QLabel(self.layoutWidget)
        self.number_2.setMinimumSize(QtCore.QSize(50, 50))
        self.number_2.setObjectName("number_2")
        self.horizontalLayout_4.addWidget(self.number_2)
        self.kaoqin_didian = QtWidgets.QLineEdit(self.layoutWidget)
        self.kaoqin_didian.setObjectName("kaoqin_didian")
        self.horizontalLayout_4.addWidget(self.kaoqin_didian)
        self.verticalLayout.addLayout(self.horizontalLayout_4)
        self.kaoqin_zhuangtailan = QtWidgets.QTextBrowser(kaoqin)
        self.kaoqin_zhuangtailan.setGeometry(QtCore.QRect(0, 610, 801, 31))
        self.kaoqin_zhuangtailan.setObjectName("kaoqin_zhuangtailan")

        self.retranslateUi(kaoqin)
        QtCore.QMetaObject.connectSlotsByName(kaoqin)

    def retranslateUi(self, kaoqin):
        _translate = QtCore.QCoreApplication.translate
        kaoqin.setWindowTitle(_translate("kaoqin", "考勤系统"))
        self.label.setText(_translate("kaoqin", "人脸识别考勤系统"))
        self.kaoqin_shijian.setText(_translate("kaoqin", "周二 4月18日 20:17:35 2021"))
        self.label_3.setText(_translate("kaoqin", "输入姓名或学号一项即可"))
        self.number.setText(_translate("kaoqin", "学号："))
        self.number_3.setText(_translate("kaoqin", "姓名："))
        self.number_2.setText(_translate("kaoqin", "地点："))
        self.kaoqin_zhuangtailan.setHtml("信息提示")
        self.new_set()
    
    def new_set(self):
        self.kaoqin_btn_kaoqin.clicked.connect(self.renlian_kaoqin)
        self.kaoqin_btn_qingjia.clicked.connect(self.qingjia_dengji)
        self.kaoqin_btn_buqian.clicked.connect(self.louqian_buqian)
        self.kaoqin_btn_xiangji.clicked.connect(self.dakai_xiangji)
         # 给按键设置提示
        self.kaoqin_btn_kaoqin.setToolTip("考勤")
        self.kaoqin_btn_qingjia.setToolTip("请假登记")
        self.kaoqin_btn_buqian.setToolTip("漏签补签")
        self.kaoqin_btn_xiangji.setToolTip("打开相机")
        
        self.xiangji_zhuangtai = 1 # 定义一个变量，用来打开和关闭相机
    
    def renlian_kaoqin(self):
        self.kaoqin_zhuangtailan.setHtml("正在进行考勤，请稍等")
        id = self.kaoqin_xuehao.text()
        name = self.kaoqin_xingming.text()
        if id:
            renlian_kaoqin = mysql(id = id) 
        elif name:
            renlian_kaoqin = mysql(name=name) 
        else:
            self.kaoqin_zhuangtailan.setHtml("请输入姓名或者学号")
            return
        state = renlian_kaoqin.select()
        if state==1:
            know_face_narray = to_narray(renlian_kaoqin.face)
            if self.xiangji_zhuangtai == 2:
                # 读取图像信息
                unknow_face_encoding = get_encoding(self.frame)
                # 对人脸信息进行对比，得到人脸对比结果 0.6为正常的对比精度
                contrast_ratio = face_recognition.compare_faces([know_face_narray], unknow_face_encoding, 0.6)

                # 人脸对比为真，则自动打卡，为假，则拒绝打卡
                if contrast_ratio[0] == True:
                    didian = self.kaoqin_didian.text()
                    if didian:
                        renlian_kaoqin.daka(didian = didian)
                        self.kaoqin_zhuangtailan.setHtml("打卡成功")
                    else:
                        self.kaoqin_zhuangtailan.setHtml("请输入打卡地点")
                else:
                    self.kaoqin_zhuangtailan.setHtml("人脸识别失败，请重新进行打卡")
            else:
                self.kaoqin_zhuangtailan.setHtml("请打开相机")

        else:
            self.kaoqin_zhuangtailan.setHtml("信息查询失败，错误代码：" + str(state))
        
    def louqian_buqian(self):
        self.kaoqin_zhuangtailan.setHtml("正在补签，请稍等")
        id = self.kaoqin_xuehao.text()
        name = self.kaoqin_xingming.text()
        didian = self.kaoqin_didian.text()
        if id:
            buqian = mysql(id = id) 
        elif name:
            buqian = mysql(name=name) 
        else:
            self.kaoqin_zhuangtailan.setHtml("请输入姓名或者学号")
            return
        if didian:
            state = buqian.buqian(didian = didian) 
            if state==1:
                self.kaoqin_zhuangtailan.setHtml("已经进行补签")
            else:
                self.kaoqin_zhuangtailan.setHtml("信息查询失败，错误代码：" + str(state))
        else:
            self.kaoqin_zhuangtailan.setHtml("请输入打卡地点")

    def dakai_xiangji(self):
        if self.xiangji_zhuangtai == 1:

            self.kaoqin_xiangji.setGeometry(QtCore.QRect(10, 100, 511, 381))
            self.kaoqin_xiangji.setObjectName("kaoqin_xiangji")
            # 打开相机

            self.capture = cv2.VideoCapture(0)  
            self.kaoqin_zhuangtailan.setHtml("相机已经打开")
            self.xiangji_zhuangtai=2
            ret, self.frame = self.capture.read()
            self.frame = cv2.resize(self.frame,(550,400))
            self.frame=cv2.cvtColor(self.frame,cv2.COLOR_BGR2RGB)
            self.Qframe=QImage(self.frame.data,self.frame.shape[1],self.frame.shape[0],self.frame.shape[1]*3,QImage.Format_RGB888)
            self.kaoqin_xiangji.setPixmap(QPixmap.fromImage(self.Qframe))
              
        else:
            self.xiangji_zhuangtai = 1 
            self.capture.release()
            self.kaoqin_xiangji.setGeometry(QtCore.QRect(1000, 100, 1001, 381))
            self.kaoqin_zhuangtailan.setHtml("相机已经关闭")

    def qingjia_dengji(self):
        self.kaoqin_zhuangtailan.setHtml("正在进行请假登记，请稍等")
        id = self.kaoqin_xuehao.text()
        name = self.kaoqin_xingming.text()
        if id:
            qingjia_dengji = mysql(id = id) 
        elif name:
            qingjia_dengji = mysql(name=name) 
        else:
            self.kaoqin_zhuangtailan.setHtml("请输入姓名或者学号")
            return
        state = qingjia_dengji.qingjia()
        if state==1:
            self.kaoqin_zhuangtailan.setHtml("已经进行请假登记")
        else:
            self.kaoqin_zhuangtailan.setHtml("信息查询失败，错误代码：" + str(state))


